﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSettings>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    initData();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initData()
{
    ui->dateGeneration->setDisplayFormat(tr("yyyy/MM/dd"));
    ui->dateGeneration->setDate(QDate::currentDate());
    ui->dateGeneration->setCalendarPopup(true);

    ui->editTitle->setText(tr("WEST BULL Securities Limited"));
    ui->editSubtitle->setText(tr("Approved Order Listing"));
    ui->editCreator->setText(tr("admin"));
    readConfig();

    // 创建一个标准的条目模型
    this->mModel = new QStandardItemModel;
    //将tableview设置成model这个标准条目模型的模板, model设置的内容都将显示在tableview上
    ui->tvContent->setModel(mModel);

    QString title = tr("Order No.,Client a/c,Client Name,B/S,Product,Price,Quantity,Input User,Limit Code Reason,Approved User&Time");
    QStringList titleList = title.split(',');
    int index = 0;
    for(const auto & str : titleList) {
        mModel->setHorizontalHeaderItem( index, new QStandardItem(str));
        ui->tvContent->setColumnWidth(index, 100);
        index ++;
    }
    QStringList aol1 = {"20230710-HK-25","C0000208","zhaoxuchang","Bid","HK:03011","8,269.000","1","xiu", "\x80\x7B\x53\x4F\x2D\x4E\x87\x65", " "};
    QStringList aol2 = {"20230710-HK-23","C0000208","zhaoxuchang","Bid","HK:03011","8,319.000","1","Xyy1", tr("离价限额(移动端、web)")," "};
    QStringList aol3 = {"20230710-HK-22","C0000208","zhaoxuchang","Bid","HK:03011","8,319.000","1","Xyy1", tr("离价限额(移动端、web)")," "};
    QStringList aol4 = {"20230710-HK-21","C0000208","zhaoxuchang","Bid","HK:03011","8,571.000","1","xiu", tr("离价限额"),"xiu 2023-07-10 17:13:55"};
    QStringList aol5 = {"20230710-HK-19","C0000208","zhaoxuchang","Bid","HK:03011","8,319.000","1","Xyy1",tr("离价限额(移动端、web)"),"xiu 2023-07-10 17:13:49"};
    m_vecApprovedOrderListOr4.push_back(aol1);
    m_vecApprovedOrderListOr4.push_back(aol2);
    m_vecApprovedOrderListOr4.push_back(aol3);
    m_vecApprovedOrderListOr4.push_back(aol4);
    m_vecApprovedOrderListOr4.push_back(aol5);

    int row = 0, col = 0;
    for (const auto item : m_vecApprovedOrderListOr4) {
        for (const QString &str : item) {
             mModel->setItem( row, col, new QStandardItem(str) );
             col ++;
        }
        row++;
        col = 0;
    }

}
void MainWindow::readConfig() {
    QSettings setting(tr("./Report.cfg"), QSettings::IniFormat);
//    QStringList groupList = setting.childGroups();
    if(setting.contains(tr("ReportInfo/title"))) {
        QString title = setting.value("ReportInfo/title").toString();
        ui->editTitle->clear();
        ui->editTitle->setText(title);

        QString subtitle = setting.value("ReportInfo/subtitle").toString();
        ui->editSubtitle->setText(subtitle);

        QString creator = setting.value("ReportInfo/creator").toString();
        ui->editCreator->setText(creator);
    }

}

void MainWindow::writeConfig() {
    QSettings settings(tr("./Report.cfg"), QSettings::IniFormat); // 当前目录的INI文件
       //GPS setting
       settings.beginGroup("ReportInfo");
       settings.setValue("title", ui->editTitle->text());
       settings.setValue("subtitle", ui->editSubtitle->text());
       settings.setValue("creator", ui->editCreator->text());
       settings.endGroup();
}

void MainWindow::on_btnSavePdf_clicked()
{
    writeConfig();
    PDF *p = nullptr;
    p = PDF_new();

    if(p == nullptr || PDF_open_file(p, "approved_order_listing_OR04.pdf") == -1) {
        qDebug() << "Error: Cannot open PDF file order_log.pdf";
        return ;
    }

    PDF_set_info(p, "Creator", "FOM");
    PDF_set_info(p, "Author", "AFI");
    PDF_set_info(p, "Title", "Approved Orders Listing");

    PDF_begin_page(p, a4_height, a4_width);	/* start a new page	*/

    SetPDFTitle(p, 1, "WEST BULL Securities Limited", "Approved Order Listing", "AE Code (0103)" );

    float y = a4_width-65;
    approvedRejectedOrderListHeader(p, 20, y);

    PDF_setrgbcolor(p,  0.0, 0.0, 0.1);
    y -= 10;
    for (const auto item : m_vecApprovedOrderListOr4) {

        if(y < 20) {
            PDF_end_page(p);
            PDF_begin_page(p, a4_height, a4_width);
            int fontTime = PDF_findfont(p, "Helvetica", "host", 0);
            if (fontTime == -1) {
                qDebug("Error: Unkown Helvetica font." );
                return ;
            }
            PDF_setfont(p, fontTime, 8);
            y = a4_width-20;
            approvedRejectedOrderListHeader(p, 20, y);
            PDF_setrgbcolor(p,  0.0, 0.0, 0.1);
            y -= 10;
        }
        approvedRejectedOrderListContent(p, item, 20, y );
        y-=15;

    }


    PDF_end_page(p);

    PDF_close(p);

    PDF_delete(p);
}


bool MainWindow::SetPDFTitle(PDF *p, int orientation, const QString &title, const QString &subTitle, const QString &code) {
    if (nullptr == p) {
        qDebug() << "PDF 指针句柄为空！ \n";
        return false;
    }
    int font = PDF_findfont(p, "Helvetica-Bold", "host", 0);

    if (font == -1) {
        qDebug("Error: Unkown Helvetica-Bold font." );
        return false;
    }
    int fontTime = PDF_findfont(p, "Helvetica", "host", 0);
    if (fontTime == -1) {
        qDebug("Error: Unkown Helvetica font." );
        return false;
    }
    PDF_setfont(p, font, 14);
    float fStringLen = PDF_stringwidth(p,  title.toUtf8().data(), font, 14);
    int width = a4_width, height = a4_height;
    if(orientation == 1) {
         width = width ^ height;
         height = width ^ height;
         width = width ^ height;
    }
    PDF_show_xy(p, title.toUtf8().data(), (width-fStringLen)/2 , height-25);
    fStringLen = PDF_stringwidth(p,  subTitle.toUtf8().data(), font, 14);
     PDF_show_xy(p, subTitle.toUtf8().data(), (width-fStringLen)/2 , height-45);
//    PDF_continue_text(p, subTitle.toUtf8().data());

    PDF_setfont(p, fontTime, 10);
    QDateTime dateTime;
    dateTime = QDateTime::currentDateTime();
    QString sDateTime = dateTime.toString("yyyyMMdd hh:mm:ss");
    qDebug()<<dateTime;
    qDebug() << sDateTime;
    PDF_show_xy(p, sDateTime.toUtf8().data(), width-90 , height-25);
    PDF_continue_text(p, code.toUtf8().data());

    return true;
}

void MainWindow::approvedRejectedOrderListHeader(PDF *p, float x, float & y) {

    int fontTime = PDF_findfont(p, "Helvetica", "host", 0);
    if (fontTime == -1) {
        qDebug("Error: Unkown Helvetica font." );
        return ;
    }
    PDF_setfont(p, fontTime, 8);

     x = 20;
    int nCol =  mModel->columnCount();
    for (int index = 0; index < nCol; ) {
        QString name = mModel->horizontalHeaderItem(index)->text();
        PDF_show_xy(p, name.toUtf8().data(), x , y);
        if (index == 0) x+= 80;
        else if (index == 2 ) x+= 120;  // "Client A/C"
        else if (index == 3 ) x+= 45; // "B/S"
//        else if (index == 4) x+= 95;
         else if (index == 8) x+= 125;
        else x += 60;
        index++;
    }

    /* Set the line width */
    PDF_setlinewidth(p, 1.0);

    /*Set the stroke color*/
    PDF_setrgbcolor(p,  0.0, 0.5, 0.5);

    x = 18;
    y -= 5;
    PDF_moveto(p, x, y);

    /* Draw a line from the current point to the supplied point */
    PDF_lineto(p, a4_height-x, y);

    /* Stroke the path using the current line width and stroke color, and
     * clear it
     */
    PDF_stroke(p);
}


void MainWindow::approvedRejectedOrderListContent(PDF *p, const QStringList & list, float x, float & y) {
        int index = 0;
        for (const QString& str : list) {
            qDebug() << str.toUtf8().constData();

            PDF_show_xy(p,  str.toUtf8().constData(), x , y);
            if (index == 0) x+= 80;
            else if (index == 2 ) x+= 120;  // "Client A/C"
            else if (index == 3 ) x+= 45; // "B/S"
//            else if (index == 4) x+= 95;
            else if (index == 8) x+= 125;
            else x += 60;
            ++ index;
        }
    }

